#include <cstdio>
#include <cstdlib>
#include <string>
#include <map>
#include <iostream>
#include <algorithm>

#define min(a, b) (((a)<(b))?(a):(b))
#define max(a, b) (((a)>(b))?(a):(b))

using namespace std;

	typedef struct {
		string name;
		int sons;
		int father;
	} PersonInfo;
	PersonInfo person[1011];
	string namelist[1011];
	int indegree[1011];
	int g[1011][1011];
	int nlines, d;
	int Ncase, ct;
	int n;


int get_id(string newname)
{
	int i;
	for (i=1; i<=n; i++)
		if (namelist[i] == newname)
			break;
	if (i>n)
	{
		++n;
		g[n][0] = 0;
		namelist[n] = newname;
		return n;
	}
	return i;
}

bool cmp(PersonInfo a, PersonInfo b)
{
	if (a.sons > b.sons) return true; else
	if (a.sons < b.sons) return false; else
	{
		if (a.name < b.name) return true; 
		else return false;
	}
}

main()
{
	int i, j, k, p, Nson;
	int root;
	string tmp;
	char tname[13];

	//debug
	//freopen("b.txt", "r", stdin);

	scanf("%d", &Ncase);
	for (ct=1; ct<=Ncase; ct++)
	{
		//memset(g, 0, sizeof(g));
		memset(indegree, 0, sizeof(indegree));
		memset(person, 0, sizeof(person));
		scanf("%d%d", &nlines, &d);
		n = 0;
		for (i=1; i<=nlines; i++)
		{
			scanf("%s%d", tname, &Nson);
			tmp = tname;
			k = get_id(tmp);
			person[k].name = tmp;
			for (j=1; j<=Nson; j++)
			{
				scanf("%s", tname);
				tmp = tname;
				p = get_id(tmp);
				person[p].name = tmp;	
				g[k][++g[k][0]] = p;
				indegree[p]++;
				person[p].father = k;
				person[p].sons = 0;
			}
		}

		if (d>=n)
		{
			cout << "Tree " << ct << ":" << endl;
			if (ct<Ncase) cout << endl;
			continue;
		}

		for (i=1; i<=n; i++)
		{
			k = i;
			for (j=1; j<=d; j++)
				if (person[k].father>0) k = person[k].father;
				else break;
			if (j>d)
				person[k].sons++;
		}

		sort(person+1, person+n+1, cmp);

		cout << "Tree " << ct << ":" << endl;
		int rank = 1;
		int first = true;
		i = 1;
		while (rank<=3 && i<=n)
		{
			if (person[i].sons == 0) break;
			if (i>1 && person[i].sons != person[i-1].sons)
			{
				rank++;
				first = true;
			}
			if (rank>3) break;
			while (first || person[i].sons == person[i-1].sons)
			{
				first = false;
				cout << person[i].name << " " << person[i].sons << endl; 
				i++;
			}
			if (i>3) break;
		}
		if (ct<Ncase) cout << endl;
	}
	return 0;
}
